# !/bin/bash

if [ -z $1 -a -z $2 ]; then
    echo "Usage: install_master.sh <server_id> <local_ip>"
    echo "server_id: eg. 1 or 2"
    echo "local_ip: local_ip"
    exit 1
fi

source ./vars.sh


MYSQL_SERVER_ID=$1

LOCAL_IP_ADDRESS=$2

echo "install mgr mysql"

echo "add group mysql and  user mysql"
if [ `grep "mysql" /etc/passwd | wc -l` -eq 0 ];then
echo "adding group mysql"
/usr/sbin/groupadd mysql

echo "adding user mysql"
/usr/sbin/useradd -g mysql mysql
else
echo "mysql user is exist"
fi


rm -rf ${data_dir}

rm -rf ${log_dir}

rm -rf ${shell_dir}

rm -rf  ${base_dir}

rm -rf /etc/profile.d/mysql.sh

if [ ! -d $data_dir ]; then
    sudo mkdir -p ${data_dir}
    sudo chown mysql:mysql ${data_dir}
	sudo chmod 0700 ${data_dir}
fi
if [ ! -d $log_dir ]; then
    sudo mkdir -p ${log_dir}
    sudo chown mysql:mysql ${log_dir}
	sudo chmod 0700 ${log_dir}
fi

echo "extract mysql tarball"
tar xvf ${work_dir}/${file_name} -C ${extract_dir}
sudo chmod 0700 ${extract_dir_name}

echo "rename tarball"
sudo mv ${extract_dir_name} ${base_dir}
sudo chown mysql:mysql ${base_dir}

echo "extract mysql-shell tarball"
tar xvf ${work_dir}/${shell_name} -C ${extract_dir}
sudo chmod 0700 ${shell_dir_name}

echo "rename mysql-shell"
sudo mv ${shell_dir_name} ${shell_dir}
sudo chown mysql:mysql ${shell_dir}

echo "backup /etc/my.cnf"
sudo cp -f ${my_cnf} ${my_cnf}.orig

echo "copy my.cnf"
sudo cp  ${work_dir}/mysql-support-files/my.cnf ${my_cnf}
sudo chmod 0644 ${my_cnf}
sudo chown mysql:mysql ${my_cnf}

echo "replace my.cnf server_id settings"
sed -i "s|{{ SERVER_ID }}|$MYSQL_SERVER_ID|g" $my_cnf

echo "replace my.cnf group_replication_local_address settings"
sed -i "s|{{ LOCAL_IP_ADDRESS }}|$LOCAL_IP_ADDRESS:33061|g" $my_cnf

echo "replace my.cnf group_replication_group_seeds settings"
sed -i "s|{{ MGR_SEEDS }}|$mgr_seeds|g" $my_cnf

echo "replace my.cnf report_host settings"
sed -i "s|{{ REPORT_HOST }}|$LOCAL_IP_ADDRESS|g" $my_cnf

echo "copy mysql PATH"
sudo cp  ${work_dir}/mysql-support-files/mysql.sh /etc/profile.d/mysql.sh


echo "copy mysql.server"
cp ${base_dir}/support-files/mysql.server /etc/init.d/mysqld

echo "mysql initialize"
${base_dir}/bin/mysqld --defaults-file=/etc/my.cnf --initialize-insecure --datadir="${data_dir}" --basedir="${base_dir}" --user=mysql 

echo "add mysqld to init.d"
sudo chmod +x /etc/init.d/mysqld

echo "mysql chkconfig"
 chkconfig --add mysqld
 chkconfig mysqld  on

echo "source mysql PATH"
source /etc/profile.d/mysql.sh

echo "start mysql service"
sudo /usr/bin/systemctl start mysqld

sleep 60

echo "crete mysql mgr MASTER "

${base_dir}/bin/mysql -uroot -f -S ${data_dir}/mysql.sock <<EOF
	CREATE USER IF NOT EXISTS ${mgr_cluster_user} IDENTIFIED WITH mysql_native_password BY '${mgr_cluster_pwd}';
	GRANT ALL ON *.* TO '${mgr_cluster_user}'@'%' WITH GRANT OPTION;
	CREATE USER IF NOT EXISTS ${mgr_user} IDENTIFIED WITH mysql_native_password BY '${mgr_user_pwd}';
	GRANT REPLICATION SLAVE, BACKUP_ADMIN ON *.* TO ${mgr_user};
	RESET MASTER;
	CHANGE MASTER TO MASTER_USER='${mgr_user}', MASTER_PASSWORD='${mgr_user_pwd}' FOR CHANNEL 'group_replication_recovery';
	SET GLOBAL group_replication_local_address = '${LOCAL_IP_ADDRESS}:33061';
	SET GLOBAL group_replication_group_seeds = '${mgr_seeds}';
	SET GLOBAL group_replication_bootstrap_group=ON;
	START GROUP_REPLICATION;
	SET GLOBAL group_replication_bootstrap_group=OFF;

EOF
